# coding=utf-8
"""
Copyright (c) Huawei Technologies Co., Ltd. 2020-2028. All rights reserved.
Description:
FileName：info.py
Author: h00568282/huangbin 
Create Date: 2025/9/20 17:09
Notes:
在已经安装的mcp servers查看mcp的详细信息
"""
from typing import List

from sys_intd.exceptions import MCPServerInfoError
from sys_intd.manager import McpServerManager


def info(server_name):
    """Display detailed information about a specific MCP server.

    Provides comprehensive details about a single MCP server, including installation instructions,
    dependencies, environment variables, and examples.

    Examples:

    \b
        sysint_ctl info github            # Show details for the GitHub server
        sysint_ctl info pinecone          # Show details for the Pinecone server
    """
    mcp_server_manager = McpServerManager()
    output_lines: List[str] = []
    output_lines.append(f"[bold green]Showing information for MCP server:[/] [bold cyan]{server_name}[/]")

    try:
        # Get the server information
        server = mcp_server_manager.get_server(server_name)
        server_metadata = mcp_server_manager.get_mcp_metadata(server_name)
        if not server or not server_metadata:
            output_lines.append(f"[yellow]Server '[bold]{server_name}[/]' not found.[/]")
            return output_lines

        # Display detailed information for this server
        _display_server_info(server_name, server.to_dict(), server_metadata, output_lines)
        return output_lines

    except Exception as e:
        raise MCPServerInfoError(f"Error retrieving information for server '{server_name}'", str(e))


def _display_server_info(server_name, server, server_metadata, output_lines):
    """Display detailed information about a server"""
    # Get server metadata
    description = server_metadata.get("description", "No description")
    mcpType = server_metadata.get("mcpType", "stdio")

    # Get server details
    command = server.get("command", "")
    args = server.get("args", [])
    env = server.get("evv", {})
    url = server.get("url", "")
    headers = server.get("headers", {})

    # Print server metadata
    output_lines.append("[bold yellow]Server Name:[/]")
    output_lines.append(f"[bold cyan]{server_name}[/]\n")
    output_lines.append("[bold yellow]Server Description:[/]")
    output_lines.append(f"[bold cyan]{description}[/]\n")

    output_lines.append("[bold yellow]Server Information:[/]")
    output_lines.append(f"command: {command}")
    output_lines.append(f"args: {', '.join(args)}")
    output_lines.append(f"url: [blue underline]{url}[/]")
    output_lines.append(f"mcpType: {mcpType}")
    output_lines.append(f"env: {env}")
    output_lines.append(f"headers: {headers}")
    output_lines.append("\n")
